package hk.polyu.dds;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.view.Menu;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * This is a fake UI for paper.
 * @author hezongjian
 */
public class DetectUI extends Activity implements SensorEventListener {
	
	
	private SensorManager mSensorManager;
	private Sensor mAccelerometer;
	private Sensor mGyroscope;
	
	TextView mTvDisplay;
	
	String sAcc;
	String sGyro;
	
	
	
	class MyImgView extends ImageView {

		public MyImgView(Context context) {
			super(context);
		}

		@Override
		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);
			
			int width = canvas.getWidth();
		    int height = canvas.getHeight();
		    
		    Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.phone);

		    canvas.drawBitmap(bmp, 175, 430, null);

		}
		
	}

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detect_ui);
        
        MyImgView img = new MyImgView(this);
        img.setImageResource(R.drawable.car);
        
        this.addContentView(img, new ViewGroup.LayoutParams(
        		ViewGroup.LayoutParams.FILL_PARENT,
        		ViewGroup.LayoutParams.FILL_PARENT));
        
        
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        
        mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        mGyroscope = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
        
		mSensorManager.registerListener(DetectUI.this, mAccelerometer, SensorManager.SENSOR_DELAY_FASTEST);
		mSensorManager.registerListener(DetectUI.this, mGyroscope, SensorManager.SENSOR_DELAY_FASTEST);
		
		
		mTvDisplay = (TextView) this.findViewById(R.id.tvShow);
        
    }
    
    
    @Override
	public void onSensorChanged(SensorEvent event) {
		
    	int type = event.sensor.getType();
    	float [] data = event.values.clone();
    	
    	String s = String.format("x=%+.3f,\ty=%+.3f,\tz=%+.3f", data[0], data[1], data[2]);
    	
		switch(type) {
        case Sensor.TYPE_ACCELEROMETER:
        	this.sAcc = "Accelerometer: " + s;
            break;
        case Sensor.TYPE_GYROSCOPE:
        	this.sGyro = "Gyroscope: " + s;
            break;
        default:
            return;
        }
		
		DetectUI.this.mTvDisplay.setText(this.sAcc + "\r\n" + this.sGyro + "\r\n" + "Phone Location: Left, Front");
		
	}
    
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_detect_ui, menu);
        return true;
    }


	@Override
	public void onAccuracyChanged(Sensor arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}
}
